<?php

namespace app\admin\controller\user;

use app\common\controller\AdminController;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="注销申请")
 */
class UnsetApply extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\user\UnsetApply();
        
        $this->assign('select_list_status', $this->model::SELECT_LIST_STATUS, true);

    }


    /**
     * @NodeAnotation(title="同意")
     */
    public function agree($id)
    {
        $row = $this->model->find($id);
        if ($row->status != 0) {
            $this->error('数据不存在');
        }
        $user = \app\admin\model\user\User::find($row->user_id);
        $row->old_user_nickname = $user->user_nickname;
        $row->old_avatar = $user->avatar;
        $row->unset_time = time();
        $row->status = 1;
        $row->save();
        
        //账号匿名化，禁止再注册
        $user->user_nickname = '账户已注销';
        $user->is_del = 1;
        $user->save();
        $this->success('已处理');
    }

    /**
     * @NodeAnotation(title="拒绝")
     */
    public function refuse($id)
    {
        $row = $this->model->find($id);
        if ($row->status != 0) {
            $this->error('数据不存在');
        }
        $row->status = -1;
        $row->save();
        $user = \app\admin\model\user\User::find($row->user_id);
        $user->status = 1;
        $user->save();
        $this->success('已处理');
    }

    /**
     * @NodeAnotation(title="恢复")
     */
    public function restore($id)
    {
        $row = $this->model->find($id);
        if ($row->status != 1) {
            $this->error('数据不存在');
        }
        $user = \app\admin\model\user\User::find($row->user_id);
        $user->is_del = 0;
        $user->user_nickname = $row->old_user_nickname;
        $user->avatar = $row->old_avatar;
        $user->save();

        $row->delete();
        $this->success('已处理');
    }

    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $this->checkPostRequest();
        $row = $this->model->find($id);
        $row->isEmpty() && $this->error('数据不存在');
        try {
            $save = $row->delete();
            $user = \app\admin\model\user\User::find($row->user_id);
            $user->status = 1;
            $user->save();
        } catch (\Exception $e) {
            $this->error('删除失败:' . $e->getMessage());
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }
    
    
}